
#import "FAMHomeTableViewtableViewCell.h"

@interface FAMHomeTableViewtableViewCell()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *FAMTableView;
@property (nonatomic, strong)FAMHomeTableHeaderFoodView *FAMHomeTableHeaderFoodView;

@end
@implementation FAMHomeTableViewtableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.FAMTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.FAMTableView.scrollEnabled = NO;
    self.FAMTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.FAMTableView registerNib:[UINib nibWithNibName:@"FAMHomeTableViewFoodCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.FAMTableView.delegate = self;
    self.FAMTableView.dataSource  = self;
    UIView *tempHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.FAMTableView.size.width, 100)];
    self.FAMHomeTableHeaderFoodView = [[FAMHomeTableHeaderFoodView alloc]init];
    [tempHeaderView addSubview:self.FAMHomeTableHeaderFoodView];
    self.FAMHomeTableHeaderFoodView.sd_layout.topEqualToView(tempHeaderView).leftEqualToView(tempHeaderView).rightEqualToView(tempHeaderView).bottomEqualToView(tempHeaderView);
    self.FAMTableView.tableHeaderView = tempHeaderView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_row == 0) {
        return _FAMHomeListModel.breakfast.count;
    }else if (_row == 1){
        return _FAMHomeListModel.lunch.count;
    }else if (_row == 2){
        return _FAMHomeListModel.dinner.count;
    }else{
        return _FAMHomeListModel.extra.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FAMHomeTableViewFoodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (_row == 0) {
        cell.FAMFoodrecordModel = _FAMHomeListModel.breakfast[indexPath.row];
    }else if(_row == 1){
        cell.FAMFoodrecordModel = _FAMHomeListModel.lunch[indexPath.row];
    }else if (_row == 2){
        cell.FAMFoodrecordModel = _FAMHomeListModel.dinner[indexPath.row];
    }else{
        cell.FAMFoodrecordModel = _FAMHomeListModel.extra[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FAMHomeFoodChangeView *homeFoodChangeView = [[FAMHomeFoodChangeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (_row == 0) {
        homeFoodChangeView.FAMFoodModel = _FAMHomeListModel.breakfast[indexPath.row];
    }else if(_row == 1){
        homeFoodChangeView.FAMFoodModel = _FAMHomeListModel.lunch[indexPath.row];
    }else if (_row == 2){
        homeFoodChangeView.FAMFoodModel = _FAMHomeListModel.dinner[indexPath.row];
    }else{
        homeFoodChangeView.FAMFoodModel = _FAMHomeListModel.extra[indexPath.row];
    }
    homeFoodChangeView.FAMDate = _FAMDate;
    [UIWindow.currentViewController.view addSubview:homeFoodChangeView];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    editingStyle = UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self FAM_deleteWithIndexPath:indexPath];
    }];
    return @[deleteAction];
}
- (void)FAM_deleteWithIndexPath:(NSIndexPath *)indexPath{
    NSString *ids;
    if (_row == 0) {
        ids = ((FAMFoodrecordModel*)_FAMHomeListModel.breakfast[indexPath.row]).ids;
    }else if(_row == 1){
        ids = ((FAMFoodrecordModel*)_FAMHomeListModel.lunch[indexPath.row]).ids;
    }else if (_row == 2){
        ids = ((FAMFoodrecordModel*)_FAMHomeListModel.dinner[indexPath.row]).ids;
    }else{
        ids = ((FAMFoodrecordModel*)_FAMHomeListModel.extra[indexPath.row]).ids;
    }
    WeakSelf(self);
    [UIWindow.currentViewController showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_DeleteFoodmateFoodrecordWithId:ids success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController performSelector:@selector(FAM_loadData:isFirst:) withObject:weakSelf.FAMDate withObject:@(YES)];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController showHint:errorInfo];
        });
    }];
}
- (void)setRow:(NSInteger)row{
    _row = row;
    if (row == 0) {
        self.FAMHomeTableHeaderFoodView.dataDic = @{
            @"icon":@"home_Breakfast",
            @"title":@"Breakfast"
        };
    }else if (row == 1){
        self.FAMHomeTableHeaderFoodView.dataDic = @{
            @"icon":@"home_Lunch",
            @"title":@"Lunch"
        };
    }else if (row == 2){
        self.FAMHomeTableHeaderFoodView.dataDic = @{
            @"icon":@"home_Dinner",
            @"title":@"Dinner"
        };
    }else{
        self.FAMHomeTableHeaderFoodView.dataDic = @{
            @"icon":@"home_Snacks",
            @"title":@"Snacks"
        };
    }
}
- (void)setFAMHomeListModel:(FAMFoodrecordListModel *)FAMHomeListModel{
    _FAMHomeListModel = FAMHomeListModel;
    if (_row == 0) {
        self.FAMHomeTableHeaderFoodView.FAMCateType = @"1";
    }else if (_row == 1){
        self.FAMHomeTableHeaderFoodView.FAMCateType = @"2";
    }else if (_row == 2){
        self.FAMHomeTableHeaderFoodView.FAMCateType = @"3";
    }else{
        self.FAMHomeTableHeaderFoodView.FAMCateType = @"4";
    }
    [self.FAMTableView reloadData];
}
- (void)setFAMDate:(NSString *)FAMDate{
    _FAMDate = FAMDate;
    self.FAMHomeTableHeaderFoodView.FAMDate = FAMDate;
}
@end
